<?php


namespace app\sonpartner\controller;


class Index extends Common
{
    /**
     * 系统首页欢迎页
     */
    public function welcomePage()
    {
        $id = $this -> id;
//        $id = 2;
        //昨日起始时间戳
        $yes_start = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $yes_end = $yes_start + 86399;
        //今日起始时间戳
        $tod_start = $yes_end + 1;
        $tod_end = $tod_start + 86399;
        $data = [];
        //昨日新增注册用户数量
        $data['yes_register'] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [$yes_start, $yes_end])
            ->count();
        //今日注册
        $data['tod_register'] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [$tod_start, $tod_end])
            ->count();
        //昨日订单数据
        //淘宝订单
        $yes_tb_order = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$yes_start), date("Y-m-d H:i:s",$yes_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $yes_jd_order = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$yes_start, $yes_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $yes_pdd_order = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$yes_start, $yes_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //昨日有效订单数量
        $data['yes_order'] = $yes_tb_order['order_num'] + $yes_jd_order['order_num'] + $yes_pdd_order['order_num'];
        //昨日成交额
        $data['yes_deal'] = round($yes_tb_order['order_deal'] + $yes_jd_order['order_deal'] + $yes_pdd_order['order_deal'],2);
        //昨日预估收益
        $data['yes_earn'] = round($yes_tb_order['order_earn'] + $yes_jd_order['order_earn'] + $yes_pdd_order['order_earn'],2);
        //今日订单数据
        //淘宝订单
        $tod_tb_order = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$tod_start), date("Y-m-d H:i:s",$tod_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $tod_jd_order = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$tod_start, $tod_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $tod_pdd_order = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$tod_start, $tod_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //今日有效订单数量
        $data['tod_order'] = $tod_tb_order['order_num'] + $tod_jd_order['order_num'] + $tod_pdd_order['order_num'];
        //昨日成交额
        $data['tod_deal'] = $tod_tb_order['order_deal'] + $tod_jd_order['order_deal'] + $tod_pdd_order['order_deal'];
        //昨日预估收益
        $data['yes_earn'] = $tod_tb_order['order_earn'] + $tod_jd_order['order_earn'] + $tod_pdd_order['order_earn'];
        //最近七日时间
        $seven_start = strtotime(date('Ymd', strtotime('-7 day')));
        $seven_end = $seven_start + 86399;
        $six_start = $seven_end + 1;
        $six_end = $six_start + 86399;
        $five_start = $six_end + 1;
        $five_end = $five_start + 86399;
        $four_start = $five_end + 1;
        $four_end = $four_start + 86399;
        $three_start = $four_end + 1;
        $three_end = $three_start + 86399;
        $two_start = $three_end + 1;
        $two_end = $two_start + 86399;
        //最近7日新增用户
        $data['seven_register'][date('m-d', $seven_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$seven_start), date("Y-m-d H:i:s",$seven_end)])
            ->count();
        $data['seven_register'][date('m-d', $six_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$six_start), date("Y-m-d H:i:s",$six_end)])
            ->count();
        $data['seven_register'][date('m-d', $five_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$five_start), date("Y-m-d H:i:s",$five_end)])
            ->count();
        $data['seven_register'][date('m-d', $four_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$four_start), date("Y-m-d H:i:s",$four_end)])
            ->count();
        $data['seven_register'][date('m-d', $three_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$three_start), date("Y-m-d H:i:s",$three_end)])
            ->count();
        $data['seven_register'][date('m-d', $two_start)] = db('user_manage')
            ->where('partner_id', $id)
            ->where('time', 'between', [date("Y-m-d H:i:s",$two_start), date("Y-m-d H:i:s",$two_end)])
            ->count();
        $data['seven_register'][date('m-d', $yes_start)] = $data['yes_register'];
        //最近七日订单数据
        //淘宝订单
        $tb_seven = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$seven_start), date("Y-m-d H:i:s",$seven_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_seven = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$seven_start, $seven_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_seven = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$seven_start, $seven_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $seven_start)] = $tb_seven['order_num'] + $jd_seven['order_num'] + $pdd_seven['order_num'];
        $data['seven_deal'][date('m-d', $seven_start)] = $tb_seven['order_deal'] + $jd_seven['order_deal'] + $pdd_seven['order_deal'];
        $data['seven_earn'][date('m-d', $seven_start)] = $tb_seven['order_earn'] + $jd_seven['order_earn'] + $pdd_seven['order_earn'];
        //淘宝订单
        $tb_six = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$six_start), date("Y-m-d H:i:s",$six_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_six = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$six_start, $six_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_six = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$six_start, $six_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $six_start)] = $tb_six['order_num'] + $jd_six['order_num'] + $pdd_six['order_num'];
        $data['seven_deal'][date('m-d', $six_start)] = $tb_six['order_deal'] + $jd_six['order_deal'] + $pdd_six['order_deal'];
        $data['seven_earn'][date('m-d', $six_start)] = $tb_six['order_earn'] + $jd_six['order_earn'] + $pdd_six['order_earn'];
        //淘宝订单
        $tb_five = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$five_start), date("Y-m-d H:i:s",$five_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_five = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$five_start, $five_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_five = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$five_start, $five_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $five_start)] = $tb_five['order_num'] + $jd_five['order_num'] + $pdd_five['order_num'];
        $data['seven_deal'][date('m-d', $five_start)] = $tb_five['order_deal'] + $jd_five['order_deal'] + $pdd_five['order_deal'];
        $data['seven_earn'][date('m-d', $five_start)] = $tb_five['order_earn'] + $jd_five['order_earn'] + $pdd_five['order_earn'];
        //淘宝订单
        $tb_four = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$four_start), date("Y-m-d H:i:s",$four_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_four = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$four_start, $four_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_four = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$four_start, $four_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $four_start)] = $tb_four['order_num'] + $jd_four['order_num'] + $pdd_four['order_num'];
        $data['seven_deal'][date('m-d', $four_start)] = $tb_four['order_deal'] + $jd_four['order_deal'] + $pdd_four['order_deal'];
        $data['seven_earn'][date('m-d', $four_start)] = $tb_four['order_earn'] + $jd_four['order_earn'] + $pdd_four['order_earn'];
        //淘宝订单
        $tb_three = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$three_start), date("Y-m-d H:i:s",$three_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_three = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$three_start, $three_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_three = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$three_start, $three_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $three_start)] = $tb_three['order_num'] + $jd_three['order_num'] + $pdd_three['order_num'];
        $data['seven_deal'][date('m-d', $three_start)] = $tb_three['order_deal'] + $jd_three['order_deal'] + $pdd_three['order_deal'];
        $data['seven_earn'][date('m-d', $three_start)] = $tb_three['order_earn'] + $jd_three['order_earn'] + $pdd_three['order_earn'];
        //淘宝订单
        $tb_two = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [date("Y-m-d H:i:s",$two_start), date("Y-m-d H:i:s",$two_end)])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //京东订单
        $jd_two = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$two_start, $two_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        //拼多多订单
        $pdd_two = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.partner_id', $id)
            ->where('order_status', 'NEQ', 0)
            ->where('order_time', 'between', [$two_start, $two_end])
            ->field('count(o.id) as order_num,sum(goods_num*goods_price) as order_deal,sum(partner_rebate_fee) as order_earn')
            ->find();
        $data['seven_order'][date('m-d', $two_start)] = $tb_two['order_num'] + $jd_two['order_num'] + $pdd_two['order_num'];
        $data['seven_deal'][date('m-d', $two_start)] = $tb_two['order_deal'] + $jd_two['order_deal'] + $pdd_two['order_deal'];
        $data['seven_earn'][date('m-d', $two_start)] = $tb_two['order_earn'] + $jd_two['order_earn'] + $pdd_two['order_earn'];
        $data['seven_order'][date('m-d', $yes_start)] = $data['yes_order'];
        $data['seven_deal'][date('m-d', $yes_start)] = $data['yes_deal'];
        $data['seven_earn'][date('m-d', $yes_start)] = $data['yes_earn'];
        returnSuccess($data, '查询成功');
    }

    /**
     * 登录日志
     */
    public function loginLog()
    {
        $uid = $this -> id;
        $input = input();
        //页数
        $page = isset($input['page']) ? $input['page'] : 1;
        $limit = 20;
        $offset = ($page - 1) * $limit;
        //获取数据总条数
        $total_num = db('login_log')
            ->where('uid', $uid)
            ->where('user_type', 1)
            ->count();
        $data = db('login_log')
            ->where('uid', $uid)
            ->where('user_type', 1)
            ->order('login_time desc')
            ->field('login_time,login_ip,login_area,login_browser')
            ->limit($offset, $limit)
            ->select();
        $page_num = ceil($total_num / $limit);
        if(!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['login_time'] = date('Y-m-d H:i:s', $value['login_time']);
            }
            returnSuccess(['data'=>$data,'total_num'=>$total_num,'page_num'=>$page_num]);
        } else {
            returnError('暂无数据');
        }
    }


    /**
     * 系统公告
     */
    public function sysNotice()
    {
        $input = input();
        $page = isset($input['page']) ? $input['page'] : 1;
        $limit = 20;
        $offset = ($page - 1) * $limit;
        $data = db('sys_notice')
            ->where('status', 1)
            ->order('add_time desc')
            ->limit($offset, $limit)
            ->field('id,content,add_time,status')
            ->select();
        $total_num = db('sys_notice')
            ->where('status', 1)
            ->count();
        $page_num = ceil($total_num / $limit);
        if(!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
            }
            returnSuccess(['data'=>$data,'total_num'=>$total_num,'page_num'=>$page_num], '查询成功');
        } else {
            returnError('暂无数据');
        }
    }
}